package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.util.date.DateUtil;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.TagEvaluateRelationDao;
import com.frogs.ucenter.entity.TagEvaluateRelation;
import com.frogs.ucenter.v1.request.TagEvaluateRelationAddRequest;
import com.frogs.ucenter.v1.request.TagEvaluateRelationListRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.PageListResponse;
import com.frogs.ucenter.v1.service.GoodsEvaluateService;
import com.frogs.ucenter.v1.service.TagEvaluateRelationService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION + "tagEvaluateRelationService")
public class TagEvaluateRelationServiceImpl implements TagEvaluateRelationService {
	Logger log = LoggerFactory.getLogger(TagBaseInfoServiceImpl.class);
	
	@Resource
    private TagEvaluateRelationDao tagEvaluateRelationDao;
    
    @Resource(name=ApiConstants.VERSION+"goodsEvaluateService")
    private GoodsEvaluateService goodsEvaluateService;

	@Override
	public Response insert(Request request) throws Exception{
		try {
			AddResponse<TagEvaluateRelation> response = new AddResponse<TagEvaluateRelation>();  
			TagEvaluateRelationAddRequest teraRequest = (TagEvaluateRelationAddRequest) request;
			int tagId = teraRequest.getTagId();
			int evId = teraRequest.getEvaluateId();
			TagEvaluateRelation tagEvaluateRelation = new TagEvaluateRelation();
			
			tagEvaluateRelation.setEvaluateId(evId);
			tagEvaluateRelation.setTagId(tagId);
			tagEvaluateRelation.setCreateTime(DateUtil.getCurrTimestamp());
			tagEvaluateRelation.setUserId(teraRequest.getUserId());
			
			tagEvaluateRelationDao.insert(tagEvaluateRelation);
			response.setInfo(tagEvaluateRelation);
			return response;
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	}

	@Override
	public Response list(Request request) throws Exception {
		try {
			TagEvaluateRelationListRequest tergRequest = (TagEvaluateRelationListRequest) request;
			int evaluateId = tergRequest.getEvaluateId();
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("aliasName", "ter");
			params.put("eqEvaluateId", evaluateId);
			PageListResponse<Map<String, Object>>  listResponse = new PageListResponse<Map<String, Object>> ();
			listResponse.setList(tagEvaluateRelationDao.listEvaluateTags(params));
			return listResponse;
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	}
}